﻿using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Epitech.Krisk.Xna.ScreenManagerLib.Widget;
using Epitech.Krisk.Xna.UI.Game.InGame;
using Epitech.Krisk.Xna.UI.Game.Utils;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Epitech.Krisk.Xna.ScreenManagerLib;

namespace Epitech.Krisk.Xna.UI.Game.Menu
{
    class LoadView : MenuScreen
    {
        RenderBackground _bg;
        private ListView _list;

        public LoadView()
            : base("Charger une partie")
        {
            // Create our menu entries.
            LayoutEntry Menu = new LayoutEntry(new Vector2(1575f, 910f));
            Menu.Alignement = LayoutEntry.TextAlignement.Right;
            MenuEntry playGameEntry = new MenuEntry("[ Lancer la partie ]");
            MenuEntry mainMenuEntry = new MenuEntry("[ Menu principal ]");

            _list = new ListView(new Vector2(910f, 175f));
            _list.DefaultColor = new Color(208, 44, 6);
            _list.SelectedColor = new Color(119, 121, 81);

            ChargeAndSaveGame.ParseGames();
            foreach (var item in ChargeAndSaveGame.GamesSave)
            {
                _list.AddElem(item);
            }
            playGameEntry.Selected += startGame;
            mainMenuEntry.Selected += displayMainMenu;

            Menu.AddEntry(playGameEntry);
            Menu.AddEntry(mainMenuEntry);
            // Add entries to the menu.
            MenuEntries.Add(_list); 
            MenuEntries.Add(Menu);

            foreach (MenuEntry item in Menu.MenuEntries)
            {
                item.FontSize = 1f;
                item.SelectedColor = new Color(119, 121, 81);
                item.DefaultColor = new Color(208, 44, 6);
                item.IsPulse = false;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            _bg.Draw(gameTime);
            base.Draw(gameTime);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            _bg.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void LoadContent()
        {
            _bg = new RenderBackground("Game/Background")
                      {
                          Content = ScreenManager.Game.Content,
                          SpriteBatch = ScreenManager.SpriteBatch,
                          ViewPort = ScreenManager.GraphicsDevice.Viewport
                      };
            _bg.LoadContent();
            base.LoadContent();
        }

        #region Handle Input


        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.Game.Exit();
        }

        protected void startGame(object sender, PlayerIndexEventArgs playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.RemoveScreen(this);
            ScreenManager.AddScreen(new InGameScreen(_list.SelectedElem), null);
        }

        protected void displayMainMenu(object sender, PlayerIndexEventArgs playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.RemoveScreen(this);
            ScreenManager.AddScreen(new MainMenu(), null);
        }

        #endregion
    }
}
